$(document).ready(function (){
require(['inputdrop'] ,function (inputdrop){

	//样式设置
	{
		$("#content_div").addClass("appbgf");
	}

	var list_url = "siem/rawEvent/queryEventList";
	var list_header = [
						{text:'',name:"t",width:3,hideSearch:"hide"},
						{text:'原始事件名称',width:13,name:"alarmName",align:"left"},
						{text:'类型',width:11,name:"alarmType",align:"left"},
						{text:'等级',width:6,name:"alarmLevel",align:"left"},
						{text:'源IP',width:12,name:"srcIpv",align:"left"},
						{text:'目的IP',width:12,name:"destIpv",align:"left"},
						{text:'数量',width:7,name:"eventCount",align:"left"},
						{text:'事件设备',width:12,name:"deviceName",align:"left"},
						{text:'事件设备IP',width:12,name:"deviceIpv",align:"left"},
						{text:'时间',width:12,name:"startDate",align:"left"}
				   ];

	var log_query_detail_url = "siem/rawEvent/queryEventTotal";

	var detail_url = "siem/rawEvent/queryEvent";

	var search_el;
	
	view_init();

	event_init();

	log_query_detail();

	function view_init()
	{
		log_list();
	}

	function event_init()
	{
		$("#search_btn").click(function(){
			if (search_el)
			{
				search_el.parent().show();
				return false;
			}
			search_init();
		});
	}

	function log_query_detail()
	{
		 //调用后台查询详情
		um_ajax_get({
			url : log_query_detail_url,
			isLoad : false,
			successCallBack : function(data){
				$("#dataform").umDataBind("render",data);
			}
		});
	}
	
	function log_list()
	{
		g_grid.render($("#table_div1"),{
			 header:list_header,
			 url:list_url,
			 isLoad : true,
			 maskObj : "#content_div",
			 hideSearch:true,
			 dbClick : detail_template_init,
			 dbIndex:1,
			 allowCheckBox:false,
			 hasBorder : false,
		 	 autoHeight : true,
		 	 showCount : true
		});
	}

	function detail_template_init(rowData)
	{
		$.ajax({
			type: "GET",
			url: "module/event_alarm/original_log_query_tpl.html",
			success: function(data) {
				g_dialog.dialog($(data).find("[id=detail_template]"), {
					width: "700px",
					title:"原始事件详细信息",
					init:init,
					isDetail:true
				});
			}
		});

		function init(el)
		{
			um_ajax_get({
				url:detail_url,
				paramObj:{eventId:rowData.eventId},
				isLoad:true,
				maskObj:"body",
				successCallBack:function(data){
					el.umDataBind("render", data[0]);
				}
			});
		}
	}

	function search_init()
	{
		$.ajax({
			type: "GET",
			url: "module/event_alarm/original_log_query_tpl.html",
			success: function(data) {
				g_dialog.dialog($(data).find("[id=search_template]"), {
					width: "900px",
					title:"原始事件查询",
					init:init,
					closeType:"hide",
					saveclick: save_click,
					autoHeight:true
				});
			}
		});

		function init(el)
		{
			search_el = el;
			g_validate.init(el);
			g_formel.code_list_render({
		   	   	key : "secEventCtg",
				secEventTypeElMul : el.find("[id=alarmType]"),
				isMultiple : true
			});
			g_formel.code_list_render({
		   	   	key : "deviceAssetTypeSiemTree",
				deviceTypeEl : el.find("[id=deviceName]"),
				onlyLastChild:false,
				noRoot:true,
				chkboxType:{ "Y": "ps", "N": "ps" }
			});
			var el_srcIp = el.find("[data-name=srcIp]");
			var el_destIp = el.find("[data-name=destIp]");
			var el_deviceIp = el.find("[data-name=deviceIp]");
			el.find("[name=srcIpType]").click(function(){
				g_validate.clear([el_srcIp]);
				var tmp = $(this).val();
				if (tmp == "1") {
					el_srcIp.attr("data-id","srcIp");
					el_srcIp.attr("validate","ipv4");
				}
				else
				{
					el_srcIp.attr("data-id","srcIpv");
					el_srcIp.attr("validate","ipv6");
				}
			});
			el.find("[name=destIpType]").click(function(){
				g_validate.clear([el_destIp]);
				var tmp = $(this).val();
				if (tmp == "1") {
					el_destIp.attr("data-id","destIp");
					el_destIp.attr("validate","ipv4");
				}
				else
				{
					el_destIp.attr("data-id","destIpv");
					el_destIp.attr("validate","ipv6");
				}
			});
			el.find("[name=deviceIpType]").click(function(){
				g_validate.clear([el_deviceIp]);
				var tmp = $(this).val();
				if (tmp == "1") {
					el_deviceIp.attr("data-id","deviceIp");
					el_deviceIp.attr("validate","ipv4");
				}
				else
				{
					el_deviceIp.attr("data-id","deviceIpv");
					el_deviceIp.attr("validate","ipv6");
				}
			});
			var startday = g_moment().add(-1,'days').format('YYYY-MM-DD HH:mm:ss');
			var today = g_moment().format('YYYY-MM-DD HH:mm:ss');
			el.find("[data-id=startDate]").val(startday);
			el.find("[data-id=endDate]").val(today); 
			//change函数
			el.find("[id=dateRange]").change(function(){
				var range = $(this).val();
				//最近一天
				if (range == 1) 
				{
					startday = g_moment().add(-1,'days').format('YYYY-MM-DD HH:mm:ss');
				}
				//最近一周
				else if (range == 2) 
				{
					startday = g_moment().add(-7,'days').format('YYYY-MM-DD HH:mm:ss'); 
				}
				//最近一月
				else if (range == 3) 
				{
					startday = g_moment().add(-30,'days').format('YYYY-MM-DD HH:mm:ss'); 
				}
				//最近一季度
				else if (range == 4) 
				{
					startday = g_moment().add(-90,'days').format('YYYY-MM-DD HH:mm:ss'); 
				}
				el.find("[data-id=startDate]").val(startday);
				el.find("[data-id=endDate]").val(today);
			});
		}

		function save_click(el,saveObj)
		{
			if(!g_validate.validate(el))
			{
				return false;
			}
			if (saveObj.alarmLevel != null) 
			{
				saveObj.alarmLevel = saveObj.alarmLevel.join(",");
			}

			g_grid.refresh($("#table_div1"),{
				queryBefore : function(queryObj){
					return saveObj;
				}
			});
			g_dialog.hide(el, "hide");
		}
	}

});
});